package com.css.zfzx.sjcj.modules.statistics.fxCasualtyEvaluationMap.repository.impl;

import com.css.zfzx.sjcj.common.vo.DivisionVo;
import com.css.zfzx.sjcj.modules.statistics.fxCasualtyEvaluationMap.repository.FxCasualtyEvaluationMapRepository;
import com.css.zfzx.sjcj.modules.statistics.fxCasualtyEvaluationMap.repository.entity.FxCasualtyEvaluationMapEntity;
import com.css.zfzx.sjcj.modules.statistics.fxEconomicLossEvaluationMap.repository.entity.FxEconomicLossEvaluationMapEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: pc-statistics
 * @Author: lhl
 * @CreateDate: 2021/5/13 11:17
 */
@Repository
public class FxCasualtyEvaluationMapRepositoryImpl implements FxCasualtyEvaluationMapRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxCasualtyEvaluationMapRepositoryImpl.class);

    @Override
    public List<FxCasualtyEvaluationMapEntity> findCreateTime(LocalDateTime createTime, int identify) {
        StringBuilder sql = new StringBuilder("select * from fx_casualty_evaluation_map");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (createTime != null) {
            whereSql.append(" and create_time > :createTime");
        } else {
            whereSql.append(" and create_time is not null");
        }
        if (identify == 1) {
            whereSql.append(" and earthquake_risk = '0'");
        } else if (identify == 2) {
            whereSql.append(" and earthquake_risk = '1'");
        } else if (identify == 3) {
            whereSql.append(" and earthquake_risk = '2'");
        } else if (identify == 4) {
            whereSql.append(" and earthquake_risk = '3'");
        }
        // 删除标识为1：有效
        whereSql.append(" and is_valid = 1");
        whereSql.append(" and update_time is null");
        whereSql.append(" order by create_time desc");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxCasualtyEvaluationMapEntity.class);

        // 时间
        if (createTime != null) {
            query.setParameter("createTime", createTime);
        }
        List<FxCasualtyEvaluationMapEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<FxCasualtyEvaluationMapEntity> findUpdateTime(LocalDateTime updateTime, int identify) {
        StringBuilder sql = new StringBuilder("select * from fx_casualty_evaluation_map");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (updateTime != null) {
            whereSql.append(" and update_time > :updateTime");
        } else {
            whereSql.append(" and update_time is not null");
        }
        if (identify == 1) {
            whereSql.append(" and earthquake_risk = '0'");
        } else if (identify == 2) {
            whereSql.append(" and earthquake_risk = '1'");
        } else if (identify == 3) {
            whereSql.append(" and earthquake_risk = '2'");
        } else if (identify == 4) {
            whereSql.append(" and earthquake_risk = '3'");
        }
        whereSql.append(" order by update_time desc");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxCasualtyEvaluationMapEntity.class);
        // 时间
        if (updateTime != null) {
            query.setParameter("updateTime", updateTime);
        }
        List<FxCasualtyEvaluationMapEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<DivisionVo> count(int identify1, int identify) {
        StringBuilder sql = new StringBuilder("select province,city,area from fx_casualty_evaluation_map");
        StringBuilder whereSql = new StringBuilder(" where is_valid = 1 ");
        if (identify1==1){
            whereSql.append(" and is_report_pg = '1'");
        }else if (identify1==2){
            whereSql.append(" and (is_report_pg = '0' or is_report_pg is null)");
        }
        if (identify == 1) {
            whereSql.append(" and earthquake_risk = '0'");
        } else if (identify == 2) {
            whereSql.append(" and earthquake_risk = '1'");
        } else if (identify == 3) {
            whereSql.append(" and earthquake_risk = '2'");
        } else if (identify == 4) {
            whereSql.append(" and earthquake_risk = '3'");
        }
        whereSql.append(" group by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());

        List<Object[]> resultList = query.getResultList();
        List<DivisionVo> voList=new ArrayList<>();
        for (Object[] objects:resultList){
            DivisionVo vo=new DivisionVo();
            vo.setProvinceName(String.valueOf(objects[0]));
            vo.setCityName(String.valueOf(objects[1]));
            vo.setAreaName(String.valueOf(objects[2]));
            voList.add(vo);
        }
        return voList;
    }
}
